#include "unp.h"


int main(int argc , char * argv[])
{
   pool_t *pool;
   mqd_t mqd;
   struct mq_attr  attr;

   unsigned int prio = 0;
   int ret,len;
   /*,len;
   while( (ret=getopt(argc,argv,":l:p:")) != -1){
     switch(ret){
        case 'l':
           len = atoi(optarg);
           break;
        case 'p':
           prio = atoi(optarg);
           break;
        case '?':
           err_msg("invalid opt %s",argv[optind]);
           break;
         case ':':
           err_msg("invalid value %s",argv[optind]);
           break;
         default :
           err_msg("no valid options %s",argv[optind]);
           break;
     } 
   }

   if(optind != (argc-1))
      err_quit("usage(%d): mq_receive  -p prio <name>",optind);
   */

   if(argc < 2)
     err_quit("usage:mq_receive <name>");

   //mqd = mq_open(argv[1],O_RDONLY|O_NONBLOCK);
   mqd = mq_open(argv[1],O_RDONLY);
   if(mqd == -1)
      err_sys("mq_open error!");

   ret = mq_getattr(mqd,&attr);
   if(ret == -1)
      err_sys("mq_getattr error!");
   len = attr.mq_msgsize;


   //pool = create_pool(1024);
   pool = create_pool(len);
   char *buff = pool_calloc(pool,len);
   ret = mq_receive(mqd,buff,len,&prio);
   if(ret == -1)
       err_sys("mq_receive error!");
   printf("rev:%d  prio:%d\n",ret,prio); 
   return 0;
}
